#pragma once
#include "Netbase.h"
#include "sync_object.h"
#include <set>

namespace XGC
{
	namespace net
	{
		class CMessageQueue	:	public IMessageQueue
		{
		public:
			CMessageQueue(void);
			~CMessageQueue(void);

		private:
			virtual INetPacket* PickMessage();
			virtual INetPacket* PopMessage();
			virtual void PushMessage( INetPacket* );
			virtual void Release();

		private:
			struct _node 
		#if defined(_DEBUG) && defined(_USE_MEM_POOL)
				: public Loki::SmallObject<>
		#endif
			{
				INetPacket* pPacket;
				volatile long		index;
				volatile _node*		next;
			};
			
			volatile _node *head, *tail, *free_head, *free_tail;

			volatile 
			_node*	alloc_node();
			void	free_node( volatile _node* p );

			volatile 
			long	m_count, m_shutdown, m_index;

			typedef std::set< size_t >	_handle_set;
			_handle_set m_handle_set;

			section	m_msg_guard;
			section	m_free_guard;
		};
	}
}